package com.cn.action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cn.domain.Book;
import com.cn.domain.Category;
import com.cn.domain.PageBean;
import com.cn.service.CategoryService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.mail.iap.Response;

/*
 * 一级分类
 */
public class CategoryAction extends ActionSupport implements ModelDriven<Category>{
	//注入CategoryService
	private CategoryService categoryService;
	//封装表单参数
	private int page=1;
	private Category category = new Category();
	@Override
	public Category getModel() {
		return category;
	}
    public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
    
   
    public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getCategoryId() throws Exception {
     Category cat = categoryService.getCategoryId(category.getCategoryid());
	/*Set<Book> books =   cat.getBooksetSet();
    for(Book book :books){
    	System.out.println(book.getBname());
    }*/
	//后台传入表单参数
		//Category cat = categoryService.getCategoryId(category.getCategoryid(),this.getPage());
    PageBean<Book> cate = categoryService.getCategoryId(category.getCategoryid(),this.getPage());
	ActionContext.getContext().getValueStack().set("cat", cat);
	  
	return "categoryindex";
   }
	//查询所有的一级分类
	public String findAllCategory(){
    List<Category> cList = categoryService.findAllCategory();
    ActionContext.getContext().getValueStack().set("cList",cList);
    
		return "adminindex";
	}
/*
 * 修改一级分类
 */
	public String editCategoryPage(){
		/*
		 * 数据回显   
		 */
	 Category cate = (Category)categoryService.getCategoryId(category.getCategoryid());
	 ActionContext.getContext().getValueStack().set("cate", cate);	
	return "editcategorypage";
	}
	/*
	 * 修改以及分类
	 */
	public String editCategory(){
		System.out.println("id="+category.getCategoryid());
		Category cate = (Category)categoryService.getCategoryId(category.getCategoryid());
		System.out.println(cate.getCategoryname());
		System.out.println("categoryname="+category.getCategoryname());
	    cate.setCategoryname(category.getCategoryname());
	    categoryService.updateCategory(cate);
		                       return "editcategory";
	}
	/*
	 * 删除一级分类
	 */
	public String deleteCategory(){
		categoryService.deleteCategory(category.getCategoryid());
		return "deletecategory";
	}
	/*
	 * 添加一级分类
	 */
	public String addCategoryPage(){
		
		return "addcategorypage";
	}
	/*
	 * 添加一级分类的逻辑处理
	 */
	public String addCategory(){
		categoryService.addCategory(category);
		return "addcategory";
	}



}
